package com.cdy.jxc_project.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;

import com.cdy.jxc_project.json.R;
import com.cdy.jxc_project.service.QiniuService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/*
 * @Project:freemovie
 * @Author:cdy(缘生)
 * @Motto:放下杂念,只为迎接明天更好的自己
 * @Date:2023/3/30 15:02
 * */
@RestController
@RequestMapping("/qiniu")
public class QiniuController {
    @Resource
    private QiniuService qiniuService;

    @SaCheckLogin
    @PostMapping(value = "/upload")
    public R uploadImage(@RequestParam("file") MultipartFile file, HttpServletRequest request) {

        if (file.isEmpty()) {
            return R.error().message("上传出现错误！");
        }
        try {
            String fileUrl = qiniuService.saveImage(file);
            return R.ok().message("文件上传成功！").data("userFace", fileUrl);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("程序出现异常！");
        }
    }
}